package git.soulbgm.utils;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 计数器
 *
 * @author SoulBGM
 * @date 2022/01/13
 */
public class Counter {

    private final static AtomicLong COUNT = new AtomicLong();
    private final static AtomicLong START_TIME = new AtomicLong(0);
    private final static AtomicLong LAST_TIME = new AtomicLong();

    /**
     * 添加 1个计数
     */
    public static void add() {
        add(1);
    }

    /**
     * 添加计数
     *
     * @param c c
     */
    public static void add(int c) {
        COUNT.addAndGet(c);
        START_TIME.compareAndSet(0, System.currentTimeMillis());
        LAST_TIME.set(System.currentTimeMillis());
    }

    /**
     * 归零
     */
    public static void makeZero() {
        COUNT.set(0);
        START_TIME.set(0);
    }

    /**
     * 获得计数
     *
     * @return {@link Long}
     */
    public static Long count() {
        return COUNT.get();
    }

    /**
     * 获得第一次计数的开始时间
     *
     * @return {@link Long}
     */
    public static Long startTime() {
        return START_TIME.get();
    }

    /**
     * 获得最后一次计数的时间
     *
     * @return {@link Long}
     */
    public static Long lastTime() {
        return LAST_TIME.get();
    }

}
